package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.EvaluateImageResponse
import com.gexiaobao.pigeon.app.model.bean.EvaluateListResponse
import com.gexiaobao.pigeon.app.model.bean.EvaluatedListResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.bean.ViewCommentsResponse
import com.gexiaobao.pigeon.app.model.param.EvaluateParam
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File


/**
 * @Author      : hxw
 * @Date        : 2023/6/27 15:43
 * @Describe    :
 */
class EvaluateViewModel : BaseViewModel() {

    private var pageSize = 20
    private var pageNo: Int = 1

    /** 评价 */
    val evaluateResult: MutableLiveData<UiState> = MutableLiveData()
    fun addGoodsReviews(body: EvaluateParam) {
        request({ apiService.addGoodsReviews(body) }, {
            val deleteState = UiState(isSuccess = true)
            evaluateResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            evaluateResult.value = deleteState
        })
    }

    /** 上传评价图片 */
    val evaluateImageResult: MutableLiveData<EvaluateImageResponse> = MutableLiveData()
    fun uploadFile(imageFile: File) {
        val body: RequestBody = imageFile.asRequestBody("multipart/form-data".toMediaTypeOrNull()) //表单类型
        val builder = MultipartBody.Builder().setType(MultipartBody.FORM)//表单类型
        builder.addFormDataPart("upLoadFile", imageFile.name, body) //添加图片数据，body创建的请求体
        val requestBody = builder.build()

        request({ apiService.uploadFile(requestBody) }, {
            evaluateImageResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 获取评价列表  */
    val viewCommentsResult: MutableLiveData<ViewCommentsResponse> = MutableLiveData()
    fun getReviewsList(isRefresh: Boolean, id: Int, evalType: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getReviewsList(id, pageNo, pageSize, evalType) }, {
            pageNo++
            viewCommentsResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取我的（待评论）订单列表 */
    val evaluateListResult: MutableLiveData<ListDataUiState<EvaluateListResponse.ListBean>> = MutableLiveData()
    fun getMyOrderCommentList(isRefresh: Boolean) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getMyOrderCommentList(pageNo, pageSize) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            evaluateListResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = isRefresh,
                    errCode = it.code,
                    errMessage = it.msg,
                    listData = arrayListOf<EvaluateListResponse.ListBean>()
                )
            evaluateListResult.value = listDataUiState
        })
    }

    /** 获取我的（已评论）订单列表 */
    val evaluatedListResult: MutableLiveData<ListDataUiState<EvaluatedListResponse.ListBean>> = MutableLiveData()
    fun getGoodsReviews(isRefresh: Boolean) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getGoodsReviews(pageNo, pageSize) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            evaluatedListResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = isRefresh,
                    errCode = it.code,
                    errMessage = it.msg,
                    listData = arrayListOf<EvaluatedListResponse.ListBean>()
                )
            evaluatedListResult.value = listDataUiState
        })
    }

}